package com.polaris.live.ui.set.cover

import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.gyf.immersionbar.ImmersionBar
import com.lxj.xpopup.XPopup
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.base2.BaseVbActivity
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.NavigationBarUtils
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.common.util.ViewUtils
import com.polaris.live.constant.LiveConst
import com.polaris.live.constant.UserConst
import com.polaris.live.databinding.ActivitySetCoverBinding
import com.polaris.live.dialog.common.CommonSureDetailDialog
import com.polaris.live.dialog.common.ConfirmCanalDetailDialog
import com.polaris.live.resp.back_resp.UserCoverResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.CommonUtils.toBoolean
import com.polaris.live.utils.StartLiveProxy
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.SetLiveImageViewModel

/**
 * 当用户设置过封面照后想更改的页面
 *
 * @author Created by 半仙 on 2023/10/31/031 15:57
 */
class SetCoverActivity : BaseVbActivity<SetLiveImageViewModel, ActivitySetCoverBinding>() {

    private var mUserCoverResp: UserCoverResp? = null

    override fun initImmersionBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .transparentNavigationBar()
            .statusBarDarkFont(true)
            .titleBarMarginTop(mBinding.bar)
            .init()
        ViewUtils.setMargin(
            mBinding.startMatch,
            bottomMargin = NavigationBarUtils.getNavigationBarHeight(this)
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        initObserve()
        initListener()

        mBinding.startMatch.text = StringUtils.getString(string.start_live_streaming)
    }

    private fun initListener() {
        mBinding.bar.setOnLeftClientListener {
            finish()
        }

        mBinding.changePhotos.setOnSingleClickListener {
            val isAuditing =
                mUserCoverResp?.status == AppConst.CoverPhoto.UNDER_REVIEW || mUserCoverResp?.status == AppConst.CoverPhoto.REVIEWING
            if (isAuditing) {
                ToastGravity.showGravity(StringUtils.getString(string.cover_auditing))
                return@setOnSingleClickListener
            }
            mViewModel.coverPhotosUpload()
        }
        mBinding.startMatch.setOnSingleClickListener {
            val setting = UserManager.getUser().setting?.liveSwitch
            if (setting != null && !setting.toBoolean()) {
                ConfirmCanalDetailDialog.show(
                    this,
                    mTitle = StringUtils.getString(string.prompt),
                    mDetail = StringUtils.getString(string.live_is_close_need_open),
                    mConfirm = StringUtils.getString(string.go_to_settings),
                    mCanal = StringUtils.getString(string.directly_open),
                    onConfirmCallback = { Router.builder(RoutePath.SET_ACTIVITY).navigation(this) },
                    onCanalCallback = {
                        mViewModel.settingModify(UserConst.UserSetting.LIVE_MODULE_SWITFCH, AppConst.YES)
                    }
                )
                return@setOnSingleClickListener
            }

            startLive()
        }
    }

    private fun initObserve() {
        mViewModel.settingLiveData.observe(this) {
            startLive()
        }
        mViewModel.mUserCoverLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mUserCoverResp = it

                if (it.url.isNullOrEmpty()) {
                    showEmpty()
                }
                Glide.with(this)
                    .loadImage(it.url)
                    .into(mBinding.cover)
                if (it.status == AppConst.CoverPhoto.UNDER_REVIEW || it.status == AppConst.CoverPhoto.REVIEWING) {
                    mBinding.llState.visibility = View.VISIBLE
                }
            }, {
                showEmpty()
            })
            dismissLoading()
        }
        mViewModel.coverUploadLiveData.observe(this) { resultState ->
            parseState(resultState, {
                Router.builder(RoutePath.SET_LIVE_IMAGE_ACTIVITY)
                    .withInt("type", LiveConst.Type.CLIENT_DEFAULT)
                    .withString("name", "")
                    .navigation(this)
            }, {
                XPopup.Builder(this)
                    .isDestroyOnDismiss(true)
                    .asCustom(
                        CommonSureDetailDialog(
                            this,
                            StringUtils.getString(string.warning),
                            it.errorMsg,
                            StringUtils.getString(string.ok)
                        )
                    )
                    .show()
            })
        }
    }

    private fun startLive() {
        StartLiveProxy.builder(this)
            .startLive(mViewModel, LiveConst.Type.ONLY_LIVE)
    }

    private fun showEmpty() {
        mBinding.coverEmpty.visibility = View.VISIBLE
    }

    override fun onResume() {
        super.onResume()
        showLoading(waitTime = 0)
        mViewModel.userCover()
    }
}
